package Controller.admin.manage;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import Controller.admin.AdminBaseServlet;
import Entity.Invoice;
import Services.WebService;

//not mapped in web.xml
public class InvoiceServlet extends AdminBaseServlet {
	protected void processRequest(HttpServletRequest request, HttpServletResponse response, WebService webServices, String action) throws ServletException,
			IOException {
		if (action.equals("list")) {
			this.listInvoices(request, response, webServices, action);
		} else if (action.equals("new")) {
			this.newInvoice(request, response, webServices, action);
		} else if (action.equals("show")) {
			this.showInvoice(request, response, webServices, action);
		} else if (action.equals("edit")) {
			this.editInvoice(request, response, webServices, action);
		} else if (action.equals("delete")) {
			this.deleteInvoice(request, response, webServices, action);
		}
	}

	private void newInvoice(HttpServletRequest request, HttpServletResponse response, WebService webServices, String action) throws IOException {
		Date date = new Date();
		boolean refund = request.getParameter("refund") == "true";
		boolean is_refunded = request.getParameter("is_refunded") == "true";
		String remarks = request.getParameter("remarks");
		String attn = request.getParameter("attn");
		String address = request.getParameter("address");

		Invoice invoice = new Invoice();
		if (request.getMethod() == "POST" && date != null && refund && is_refunded && remarks != null
				&& attn != null && address != null) {
		    invoice.setUser(webServices.getCurrentUser());
		    invoice.setDate(date);
		    invoice.setRefund(refund);
    		invoice.setIsRefunded(is_refunded);
		    invoice.setRemarks(remarks);
		    invoice.setAttn(attn);
		    invoice.setAddress(address);
			webServices.getEntityManager().newInvoice(invoice);
		    
			response.sendRedirect("");
		} else {
			this.forward(request, response, webServices, action, "/admin/manage/invoice.new.jsp");
		}
	}

	private void listInvoices(HttpServletRequest request, HttpServletResponse response, WebService webServices, String action) {
		ArrayList<Invoice> invoiceList = webServices.getEntityManager().getInvoices();
		request.setAttribute("invoices", invoiceList);
		this.forward(request, response, webServices, action, "/admin/manage/invoice.list.jsp");
	}

	private void showInvoice(HttpServletRequest request, HttpServletResponse response, WebService webServices, String action) {
		int invoiceID = Integer.valueOf(request.getParameter("invoiceID"));
		Invoice invoice = webServices.getEntityManager().getInvoice(invoiceID);

		request.setAttribute("invoice", invoice);
		this.forward(request, response, webServices, action, "/admin/manage/invoice.list.idv.jsp");
	}

	private void editInvoice(HttpServletRequest request, HttpServletResponse response, WebService webServices, String action) throws IOException {
		int invoiceID = Integer.valueOf(request.getParameter("invoiceID"));
		int userID = Integer.valueOf(request.getParameter("userID"));
		Date date = new Date();
		boolean refund = request.getParameter("refund") == "true";
		boolean is_refunded = request.getParameter("is_refunded") == "true";
		String remarks = request.getParameter("remarks");
		String attn = request.getParameter("attn");
		String address = request.getParameter("address");

		Invoice invoice = new Invoice();
		if (request.getMethod() == "POST" && date != null && refund && is_refunded && remarks != null
				&& attn != null && address != null) {
			invoice.setId(invoiceID);
			invoice.getUser().setId(userID);
		    invoice.setDate(date);
		    invoice.setRefund(refund);
    		invoice.setIsRefunded(is_refunded);
		    invoice.setRemarks(remarks);
		    invoice.setAttn(attn);
		    invoice.setAddress(address);

			webServices.getEntityManager().updateInvoice(invoice);
			response.sendRedirect("./invoice?action=show&invoiceID=" + invoiceID);
		} else {
			invoice = webServices.getEntityManager().getInvoice(invoiceID);

			request.setAttribute("invoice", invoice);
			this.forward(request, response, webServices, action, "/admin/manage/invoice.edit.idv.jsp");
		}
	}

	private void deleteInvoice(HttpServletRequest request, HttpServletResponse response, WebService webServices, String action) {
		int invoiceID = Integer.valueOf(request.getParameter("invoiceID"));
		Invoice invoice = new Invoice();

		if (request.getMethod() == "POST" && invoiceID > 0) {
			invoice.setId(invoiceID);

			webServices.getEntityManager().deleteInvoice(invoice);
			this.forward(request, response, webServices, action, "/admin/manage/invoice.delete.done.jsp");
		} else {
			invoice = webServices.getEntityManager().getInvoice(invoiceID);

			request.setAttribute("invoice", invoice);
			this.forward(request, response, webServices, action, "/admin/manage/invoice.delete.confirm.jsp");
		}
	}
}
